package cn.kys.modules.system.repository;

import cn.kys.modules.system.domain.User;
import cn.kys.modules.system.pojo.GeometryPojo;
import cn.kys.modules.system.pojo.SubtaskGeom;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;


public interface ImportShp2PgRepository {

    @Select("SELECT tablename FROM pg_tables WHERE schemaname = 'public'")
    List<Map> pgTableList();

    @Update("DROP TABLE IF EXISTS ${tableName}")
    boolean delTable(@Param("tableName") String tableName);

    @Delete("DELETE FROM public.t_table_dept WHERE  tablename = #{tableName} and dept_id=#{dept_id}")
    boolean delTableInTableDept(@Param("tableName") String tableName, @Param("dept_id") String dept_id);

    @Select("SELECT * FROM t_layer WHERE name=#{tableName}")
    List<Map> findTableNameInLayerTable(@Param("tableName") String tableName);

    @Select("${sqlStr}")
    Map exeSql(@Param("sqlStr") String sqlStr);

    @Select("${sqlStr}")
    List exeSqlList(@Param("sqlStr") String sqlStr);

    @Select("${sqlStr}")
    int exeSqlInt(@Param("sqlStr") String sqlStr);

    @Select("SELECT gid,ST_AsText(ST_Transform(ST_Multi(ST_Buffer(ST_Transform(geom, 4547), 0.3)), 4326)) AS multipolygon FROM ${tableName}")
    List<GeometryPojo> findGeo(@Param("tableName") String tableName);

    @Update("UPDATE ${tableName} SET geom = ST_SetSRID('${geomText}'::geometry, 4326) WHERE gid=#{gid}")
    void updatGeom(@Param("tableName") String tableName, @Param("geomText") String geomText, @Param("gid") Integer gid);

    @Select("SELECT DISTINCT(GeometryType(geom)) FROM ${tableName}  WHERE geom IS NOT NULL")
    String findGeomType(@Param("tableName") String tableName);

    Integer insert(SubtaskGeom subtaskGeom);

    @Select("SELECT ST_AsText(ST_Transform(ST_Multi(ST_Buffer(ST_Transform(geom, 4547), 0.3)), 4326)) AS multipolygon FROM t_subtask_geom WHERE id=#{id}")
    GeometryPojo bufferGeom( @Param("id") Integer id);

    @Update("UPDATE t_subtask_geom SET geom = ST_SetSRID('${geomText}'::geometry, 4326) WHERE id=#{id}")
    void toMultiPolygon(@Param("geomText") String geomText, @Param("id") Integer id);


    @Select("SELECT ST_Union(ST_Transform(ST_Multi(ST_Buffer(ST_Transform(geom, 4547), 0.3)), 4326)) AS multipolygon FROM t_subtask_geom WHERE id=#{id}")
    GeometryPojo Test( @Param("id") Integer id);

}
